package com.jesse.eclgy.plug.dao;

import com.jesse.eclgy.plug.db.RecordSetPlus;
import com.jesse.eclgy.plug.wrapper.*;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import weaver.soa.workflow.request.RequestInfo;
import java.util.*;

/**
 * 流程DAO类型
 *
 * @Author: jesse
 * @Date: 2023/6/3 16:13
 */
public class WorkFlowDao {

    @Getter
    private RecordSetPlus rsp;

    public WorkFlowDao()
    {
        this.rsp = new RecordSetPlus();
    }


    public static WorkFlowDao builder()
    {
        return new WorkFlowDao();
    }


    /**
     * 根据 RequestInfo  查询 流程主表
     *
     * @param requestInfo
     * @return
     */
    public Map<String, Object> queryByRequestInfo(RequestInfo requestInfo)
    {
        return queryByRequestId(requestInfo.getRequestid(), requestInfo.getRequestManager().getBillTableName(), true);
    }


    /**
     * 根据RequestInfo 查询流程明细表
     *
     * @param requestInfo
     * @return
     */
    public Map<Integer, Map<String, String>> queryDetailByRequestInfo(RequestInfo requestInfo)
    {
        return queryDetailByRequestInfo(requestInfo,
                1, true);
    }

    /**
     * 根据RequestInfo 查询流程明细表
     *
     * @param requestInfo
     * @param dtIndex
     * @param isConvertMap
     * @return
     */
    public Map<Integer, Map<String, String>> queryDetailByRequestInfo(RequestInfo requestInfo, int dtIndex,
                                                                      boolean isConvertMap)
    {
        return queryDetailByRequestId(requestInfo.getRequestid(), requestInfo.getRequestManager().getBillTableName(),
                dtIndex, isConvertMap);
    }


    /**
     * 根据requestId 查询 得到 MAP
     *
     * @param requestId
     * @param tableName
     */
    public Map<String, Object> queryByRequestId(Object requestId, String tableName)
    {
        return queryByRequestId(requestId, tableName, true);
    }

    /**
     * 根据主表信息 获取MAP 结果
     *
     * @param requestId
     * @param tableName
     * @param isConvertMap
     * @return
     */
    public Map<String, Object> queryByRequestId(Object requestId, String tableName, boolean isConvertMap)
    {
        final QueryWrapper wrapper = QueryWrapper.builder();
        wrapper.tableName(tableName);
        wrapper.conditionEQ("requestId", requestId);
        this.executeQuery(wrapper);
        return isConvertMap ? convertToMainMap() : new HashMap<>();
    }

    /**
     * 查询明细表
     *
     * @param mainId
     * @param mainTableName
     * @param dtIndex
     * @return
     */
    public Map<Integer, Map<String, String>> queryDetailByMainId(Object mainId, String mainTableName, int dtIndex)
    {
        return queryDetailByMainId(mainId, mainTableName + "_dt" + dtIndex, true);
    }

    public Map<Integer, Map<String, String>> queryDetailByMainId(Object mainId, String mainTableName, int dtIndex,
                                                                 boolean isConvertMap)
    {
        return queryDetailByMainId(mainId, mainTableName + "_dt" + dtIndex, isConvertMap);
    }

    /**
     * 生成子查询
     *
     * @param requestid
     * @param mainTableName
     * @param dtIndex
     * @return
     */

    public Map<Integer, Map<String, String>> queryDetailByRequestId(Object requestid, String mainTableName,
                                                                    int dtIndex)
    {
        return queryDetailByRequestId(requestid, mainTableName, dtIndex, true);
    }

    public Map<Integer, Map<String, String>> queryDetailByRequestId(Object requestid, String mainTableName,
                                                                    int dtIndex, boolean isConvertMap)
    {

        QueryWrapper wrapper = QueryWrapper.builder();
        wrapper.tableName(dtIndex < 1 ? mainTableName + "_dt1" : mainTableName + "_dt" + dtIndex);
        QueryWrapper childCondition = QueryWrapper.builder()
                .column("id")
                .tableName(mainTableName)
                .conditionEQ("requestid", requestid);
        ConditionWrapper conditionWrapper = ConditionWrapper.builder();
        conditionWrapper.condition("mainId = ", childCondition);
        wrapper.condition(conditionWrapper);
        this.executeQuery(wrapper);
        return isConvertMap ? convertToDetailMap() : new HashMap<>();
    }

    public Map<String, Object> convertToMainMap()
    {
        Map<String, Object> data = new LinkedHashMap<>();
        if (this.rsp.next())
        {
            for (String col : this.rsp.getColumnName())
            {
                if (StringUtils.equalsIgnoreCase(col, "id") || StringUtils.equalsIgnoreCase(col, "requestId"))
                {
                    data.put(col, this.rsp.getInt(col));
                } else
                {
                    data.put(col, this.rsp.getString(col));
                }
            }
        }
        return data;
    }

    public Map<Integer, Map<String, String>> convertToDetailMap()
    {
        Map<Integer, Map<String, String>> result = new HashMap<>();
        while (this.rsp.next())
        {
            Map<String, String> rowData = new LinkedHashMap<>();
            for (String col : this.rsp.getColumnName())
            {
                rowData.put(col, this.rsp.getString(col));
            }
            result.put(this.rsp.getInt("id"), rowData);
        }
        return result;
    }


    /**
     * 查询明细表
     *
     * @param mainId
     * @param detailTableName
     * @return
     */
    private Map<Integer, Map<String, String>> queryDetailByMainId(Object mainId, String detailTableName,
                                                                  boolean isConvertMap)
    {
        final QueryWrapper wrapper = QueryWrapper.builder();
        wrapper.tableName(detailTableName);
        wrapper.conditionEQ("mainid", mainId);
        this.executeQuery(wrapper);
        return isConvertMap ? convertToDetailMap() : new HashMap<>();
    }


    /**
     * 根据requestId更新
     *
     * @param updateFieldMap
     * @return
     */
    public boolean updateByRequestId(String tableName, Map<String, Object> updateFieldMap, Object requestId)
    {
        final UpdateWrapper wrapper = UpdateWrapper.builder();
        wrapper.tableName(tableName);
        wrapper.set(updateFieldMap);
        wrapper.conditionEQ("requestId", requestId);
        return this.executeWrapper(wrapper);
    }

    /**
     * 更新流程
     * @param requestInfo
     * @param updateFieldMap
     * @return
     */
    public boolean updateByRequestInfo(RequestInfo requestInfo, Map<String, Object> updateFieldMap)
    {
        return this.updateByRequestId(requestInfo.getRequestManager().getBillTableName(), updateFieldMap,
                requestInfo.getRequestid());
    }


    /**
     * 更新主表字段
     *
     * @param field
     * @param value
     * @param requestInfo
     * @return
     */
    public boolean updateByRequestInfo(RequestInfo requestInfo, String field, Object value)
    {
        return this.updateByRequestId(requestInfo.getRequestManager().getBillTableName(), field, value,
                requestInfo.getRequestid());
    }


    public boolean updateByRequestId(String tableName, String field, Object value, Object requestId)
    {
        final UpdateWrapper wrapper = UpdateWrapper.builder();
        wrapper.tableName(tableName);
        wrapper.set(field, value);
        wrapper.conditionEQ("requestId", requestId);
        return this.executeWrapper(wrapper);
    }


    /**
     * 更新执行 包装器
     *
     * @param wrapper
     * @return
     */

    public Boolean executeWrapper(IWrapper wrapper)
    {
        return this.rsp.executeUpdate(wrapper);
    }

    /**
     * 执行查询包装器
     *
     * @param wrapper
     * @return
     */
    public Boolean executeQuery(QueryWrapper wrapper)
    {
        return this.rsp.executeQuery(wrapper);
    }
}
